package interpreter.bytecodes;

import interpreter.VirtualMachine;
import java.util.NoSuchElementException;
import java.util.Vector;

/**
 *
 * @author Kristofer Selbekk
 */
public class LitByteCode extends ByteCode {

    private int value;
    private String id; // May or may not be used

    /**
     * Initializes the byte code with 1-2 arguments
     */
    public void init( Vector<String> args ) {
        try {
            value = Integer.parseInt(args.get(0));
            
            if( args.size() >= 2)
                id = args.get(1);
        } catch( NumberFormatException nfe ) {
            // If bad format
            System.out.println("Bad format!");
            System.exit(-1);
        } catch( NoSuchElementException nse ) {
            // If empty argument
            System.out.println("Empty vector!");
            System.exit(-1);
        } catch( NullPointerException npe ) {
            // If argument missing
            System.out.println("Argument given = null.");
            System.exit(-1);
        }
    }

    /**
     * Action: Pushes the literal value on the runStack
     *
     * @param rt the virtual machine reference
     */
    public void exec( VirtualMachine rt ) {
        rt.pushOnStack( value );
    }

    public Object getArgs() {
        String args = value+"";
        if( id != null )
            args += " "+id;
        return args;
    }

    public String toString() {
        return super.toString() + "\tint " + id;
    }
}